package JUC.blockQueue;

import org.junit.Test;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author mubei
 * @date 2021/11/21
 * @description
 */
public class TestBlock {

    /*
    * 抛出异常
    * */
    @Test()
    public void test1() {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(3);
        System.out.println(queue.add("a"));
        System.out.println(queue.add("c"));
        System.out.println(queue.add("d"));

        System.out.println(queue.element());

        //java.lang.IllegalStateException: Queue full
        //System.out.println(queue.add("d"));

        System.out.println(queue.remove());
        System.out.println(queue.remove());
        System.out.println(queue.remove());

    }

    @Test()
    public void test2() {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(3);
        System.out.println(queue.offer("a"));
        System.out.println(queue.offer("a"));
        System.out.println(queue.offer("a"));


        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.peek());
        System.out.println(queue.poll());
    }


    @Test()
    public void test3() throws InterruptedException {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(3);
        queue.put("a");
        queue.put("d");
        // 会一种等待
        queue.put("c");

        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take());
    }

    @Test()
    public void test4() throws InterruptedException {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(3);
        queue.offer("a", 1,TimeUnit.SECONDS);
        queue.offer("a", 1,TimeUnit.SECONDS);
        queue.offer("a", 1,TimeUnit.SECONDS);
        queue.offer("a", 1,TimeUnit.SECONDS);
    }

}
